package com.lyl.security.module.security.service;

import cn.hutool.core.lang.Assert;
import com.lyl.security.common.exception.BizException;
import com.lyl.security.module.security.model.pojo.LoginDetails;
import com.lyl.security.module.security.model.pojo.User;
import com.lyl.security.module.security.service.UserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 * @author Mr.Luo
 * @date 2022-09-05 10:25
 * @desc
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class UserDetailsServiceImpl implements UserDetailsService {
    private final UserService userService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        log.info("方法名: loadUserByUsername,  请求参数: username = [" + username + "]");
        User user = userService.findByUsername(username);
        Assert.notNull(user,() -> new BizException("找不到用户"));
        return new LoginDetails(user);
    }
}
